﻿using UnityEngine;
using System.Collections;

public class CharacterController2D : MonoBehaviour 
{
    public LayerMask PlatformMask;
    public ControllerParameters2D DefaultParameters;

    public Vector2 Velocity { get { return _velocity; } }
    public ControllerParameters2D Parameters { get { return _overrideParameters ?? DefaultParameters; } }

    private Vector2 _velocity;
    private Transform _transform;
    private Vector3 _localScale;
    private BoxCollider2D _boxCollider;
    private ControllerParameters2D _overrideParameters;

    public void Awake()
    {
        _transform = transform;
        _localScale = transform.localScale;
        _boxCollider = GetComponent<BoxCollider2D>();
    }

    public void AddForce(Vector2 force)
    {
        _velocity += force;
    }
    public void SetForce(Vector2 force)
    {
        _velocity = force;
    }

    public void SetVerticalForce(float force )
    {
        _velocity.x = force;
    }

    public void SetHorizontalForce(float force)
    {
        _velocity.y = force;
    }

    public void LateUpdate()
    {
        _velocity.x += 0;
        _velocity.y += 0;
        Move(Velocity * Time.deltaTime);
    }

    public void Move(Vector2 deltaMovement)
    {


        _transform.Translate(deltaMovement, Space.World);

        if (Time.deltaTime > 0)
            _velocity = deltaMovement / Time.deltaTime;

        _velocity.x = Mathf.Min(_velocity.x, Parameters.MaxVelocity.x);
        _velocity.y = Mathf.Min(_velocity.y, Parameters.MaxVelocity.y);

    }

}
